#include <Arduino.h>
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip
#include <SPI.h>
#include <WiFi.h>
#include <Adafruit_AHTX0.h>
#include "BMP.h"

//绑定两个灯
#define REDLED1 12 // WIFI开始连接，亮灯，连接成功后，灯灭
#define REDLED2 13 // WIFI开始连接，亮灯，连接成功后，灯灭

#define NTP1 "ntp1.aliyun.com"
#define NTP2 "ntp2.aliyun.com"
#define NTP3 "ntp3.aliyun.com"
#define WIDTH 160
#define HEIGHT 80
#define I2C_SDA 4 //传感器I2C使用的管脚
#define I2C_SCL 5

const char *ssid = "TGYiPhone";
const char *password = "1234567809"; // WIFI密码

TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
TFT_eSprite spr = TFT_eSprite(&tft);
Adafruit_AHTX0 aht;
WiFiClient client;
int test = 1;

void setClock0() //详见http://t.csdn.cn/lNVIv
{
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo))
  {
    //如果获取失败，就开启网络对时
    Serial.println("Failed to obtain time");
    configTime(8 * 3600, 0, NTP1, NTP2, NTP3);
    return;
  }
  spr.drawRect(0, 0, WIDTH, HEIGHT, TFT_BLACK);
  spr.fillSprite(TFT_BLACK);
  spr.setTextColor(TFT_YELLOW, TFT_BLACK);
  spr.setTextFont(2);
  spr.setTextSize(2);

  spr.setCursor(24, 24);
  spr.println(&timeinfo, "%T"); //显示时分秒：hh:mm:ss
  spr.println(test);            //显示时分秒：hh:mm:ss
  //上面的这些内容下先被输出到Sprite中，然后再一次性输出到屏幕上
  spr.pushSprite(0, 0); //将精灵的内容一次性推向屏幕
}

void refresh()
{
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo))
  {
    //如果获取失败，就开启网络对时
    Serial.println("Failed to obtain time");
    configTime(8 * 3600, 0, NTP1, NTP2, NTP3);
    return;
  }
  if (timeinfo.tm_sec % 5 == 0)
  {
    test = timeinfo.tm_sec; //把时间数据放入test
  }
}

void setup()
{
  Serial.begin(115200);

  pinMode(REDLED1, OUTPUT);
  pinMode(REDLED2, OUTPUT);

  Wire.begin(I2C_SDA, I2C_SCL);
  Serial.println("Adafruit AHT10 demo!");

  if (!aht.begin())
  {
    Serial.println("Could not find AHT? Check wiring");
    while (1)
      delay(10);
  }
  Serial.println("AHT10 found");

  tft.init();
  tft.setRotation(3);
  tft.setTextFont(2);

  // Create a sprite of defined size
  // 在这里创建sprite对象，宽度和高度和屏幕一样大小。
  spr.createSprite(WIDTH, HEIGHT);

  // Clear the TFT screen to black
  tft.fillScreen(TFT_BLACK);
  tft.setTextColor(TFT_YELLOW, TFT_BLACK);
  // tft.setTextSize(1);
  tft.setCursor(25, 12);
  tft.println("Power by");
  tft.setCursor(25, 28);
  tft.setTextSize(2);
  tft.println("Arduino");
  delay(2000);

  WiFi.begin(ssid, password);
  Serial.println();
  Serial.print("WIFI Connecting");
  tft.fillScreen(TFT_BLACK); // LCD清空
  tft.setCursor(0, 0);
  tft.setTextSize(1);
  tft.print("WIFI Connecting");

  while (WiFi.status() != WL_CONNECTED)
  {
    digitalWrite(REDLED1, 1);
    delay(500);
    Serial.print(".");
    tft.print(".");
    digitalWrite(REDLED1, 0);
  }
  // 从网络时间服务器上获取并设置时间
  configTime(8 * 3600, 0, NTP1, NTP2, NTP3);
  tft.println();
  tft.println("WiFi connected!");
  // delay(500);
  tft.fillScreen(TFT_BLACK); // LCD清空
  tft.setCursor(4, 20);
  tft.setTextSize(2);
  tft.println("Ready!!! GO");
  tft.fillScreen(TFT_BLACK);
}

void loop()
{
  setClock0();
  delay(500);
  //测试怎么在loop里面刷新数据是可行的
  refresh();
  digitalWrite(REDLED1, !digitalRead(REDLED1));
  digitalWrite(REDLED2, !digitalRead(REDLED2));
}
